﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Legend.Component.Common;
using Legend.Component.Interface;
using Legend.Component.PowerBall;
using Microsoft.Xna.Framework;
using Legend.Object;

namespace Legend.Component.Hero
{
    public class HeroDamaging : IDamaging
    {
        class ReviveState : IState
        {
            private GameObject owner;
            private bool flash = false;
            private IDamaging.DamageType damageType;

            public ReviveState(GameObject owner)
            {
                this.owner = owner;
            }
            public int ActivationTime
            {
                get { return 0; }
            }

            public int Duration
            {
                get { return 3000; }
            }

            public int Pulse
            {
                get { return 500; }
            }

            public void OnActivated()
            {
                IDamaging damaging = owner.FindComponent<IDamaging>();
                damageType = damaging.DamageDisposition;
                damaging.DamageDisposition = DamageType.God;
            }

            public void OnDeactivated()
            {
                IRenderable render = owner.FindComponent<IRenderable>();
                render.AmbientColor = new Vector3(1, 1, 1);

                IDamaging damaging = owner.FindComponent<IDamaging>();
                damaging.DamageDisposition = damageType;
            }

            public void OnPulse(int elapsedTime)
            {
                IRenderable render = owner.FindComponent<IRenderable>();
                if (flash)
                {
                    render.AmbientColor = new Vector3(1, 1, 1);
                    flash = false;
                }
                else
                {
                    render.AmbientColor = new Vector3(0.5f, 0.5f, 0.5f);
                    flash = true;
                }
            }
        }

        class PheonixState : IState
        {
            private GameObject owner;
            private Vector3 oldColor;
            private float oldSpeed;
            private const string boardTitle = "WhalWhal";
            public PheonixState(GameObject owner)
            {
                this.owner = owner;
            }

            public int ActivationTime
            {
                get { return 0; }
            }

            public int Duration
            {
                get { return 30000; }
            }

            public int Pulse
            {
                get { return 1000; }
            }

            public void OnActivated()
            {
                IRenderable render = owner.FindComponent<IRenderable>();
                oldColor = render.AmbientColor;
                render.AmbientColor = new Vector3(1, 0, 0);

                KeyboardProcessor processor = owner.FindComponent<IInputProcessor>() as KeyboardProcessor;
                oldSpeed = processor.MaxSpeed;
                processor.MaxSpeed = 500;

                FirstWeapon weapon = owner.FindComponent<FirstWeapon>();
                weapon.Enable = false;

                GameObjectManager.SetBoardValue(boardTitle, 30);
            }

            public void OnDeactivated()
            {
                IRenderable render = owner.FindComponent<IRenderable>();
                render.AmbientColor = oldColor;

                KeyboardProcessor processor = owner.FindComponent<IInputProcessor>() as KeyboardProcessor;
                processor.MaxSpeed = oldSpeed;

                FirstWeapon weapon = owner.FindComponent<FirstWeapon>();
                weapon.Enable = true;

                GameObjectManager.RemoveBoardValue(boardTitle);
            }

            public void OnPulse(int elapsedTime)
            {
                GameObjectManager.AddBoardValue(boardTitle, -1);
            }
        }

        public HeroDamaging()
        {
            DamageDisposition = DamageType.Hero | DamageType.HeroFriendly;
        }

        public override string Status
        {
            get { return ""; }
        }
        public override ResolveResult ResolveDamaging(IDamaging.DamageType fromDisposition, float fromDamage, string fromStatus)
        {
            if (DamageDisposition == DamageType.God)
            {
                return ResolveResult.Continue;
            }

            IStateManager stateManager = Owner.FindComponent<IStateManager>();
            if ((fromDisposition & DamageType.EnemyFriendly) != 0)
            {
                if (stateManager.FindState<PheonixState>())
                {
                    return ResolveResult.Continue;
                }

                if (GameObjectManager.AddBoardValue("Life", -1) > 0)
                {
                    IMovement movement = Owner.FindComponent<IMovement>();
                    movement.Position = new Vector3(0, 0, 5);
                    stateManager.AddState(new ReviveState(Owner));
                    
                    return ResolveResult.Continue;
                }
                else
                {
                    return ResolveResult.Disappear;
                }
            }

            if ((fromDisposition & DamageType.PowerBall) != 0)
            {
                stateManager.Clear();
                if (fromStatus == PowerBallDamaging.PowerBallType.Pheonix.ToString())
                {
                    stateManager.AddState(new PheonixState(Owner));
                }
                else if (fromStatus == PowerBallDamaging.PowerBallType.Shield.ToString())
                {
                }
            }

            return ResolveResult.Continue;
        }
    }
}
